package com.yiwei.jmx.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.CheckCodeBean;
import com.yiwei.jmx.api.bean.ResetPassBean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.MD5;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.UI;
import com.yiwei.jmx.view.FlowViewHorizontal;
import com.yiwei.jmx.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class ChangePassStepTwoActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.step_view)
    FlowViewHorizontal stepView;
    @BindView(R.id.edit_pass)
    EditText editPass;
    @BindView(R.id.btn_pass_clear)
    ImageView btnPassClear;
    @BindView(R.id.btn_pass_open)
    ImageView btnPassOpen;
    @BindView(R.id.edit_repass)
    EditText editRepass;
    @BindView(R.id.btn_repass_clear)
    ImageView btnRepassClear;
    @BindView(R.id.btn_repass_open)
    ImageView btnRepassOpen;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private Unbinder unbiner;
    private boolean isChecked = false;
    private boolean repass_isChecked = false;
    private String mobile;
    private String token;
    private String code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pass_step_two);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("修改密码");
        stepView.setProgress(2, 2, new String[]{"验证手机号", "修改密码"}, null);
        mobile = getIntent().getStringExtra("mobile");
        token = getIntent().getStringExtra("token");
        code = getIntent().getStringExtra("code");
    }

    @OnTextChanged({R.id.edit_pass, R.id.edit_repass})
    void onTextChange(Editable s) {
        if (!TextUtils.isEmpty(UI.toString(editPass)) && !TextUtils.isEmpty(UI.toString(editRepass))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    private void getResetPass(String password) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        ResetPassBean bean = new ResetPassBean();
        bean.mobile = mobile;
        bean.password = MD5.getMD5str(password);
        bean.token = token;
        bean.verify_code = code;
        Api.api_service.getResetPass(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "修改成功");
                    finish();
                });
    }


    @OnClick({R.id.btn_back, R.id.btn_pass_clear, R.id.btn_pass_open, R.id.btn_repass_clear, R.id.btn_repass_open, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_pass_clear:
                editPass.setText("");
                break;
            case R.id.btn_pass_open:
                isChecked = !isChecked;
                if (isChecked) {
                    //如果选中，显示密码
                    editPass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    btnPassOpen.setImageResource(R.mipmap.img_eye_open);
                } else {
                    //否则隐藏密码
                    editPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    btnPassOpen.setImageResource(R.mipmap.img_eye_close);
                }

                break;
            case R.id.btn_repass_clear:
                editRepass.setText("");
                break;
            case R.id.btn_repass_open:
                repass_isChecked = !repass_isChecked;
                if (repass_isChecked) {
                    //如果选中，显示密码
                    editRepass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    btnRepassOpen.setImageResource(R.mipmap.img_eye_open);
                } else {
                    //否则隐藏密码
                    editRepass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    btnRepassOpen.setImageResource(R.mipmap.img_eye_close);
                }
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editPass))) {
                    ToastUtil.makeText(this, "密码不能为空");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editRepass))) {
                    ToastUtil.makeText(this, "重复密码不能为空");
                    return;
                }
                if (!UI.toString(editPass).equals(UI.toString(editRepass))) {
                    ToastUtil.makeText(this, "两次输入密码不一致");
                    return;
                }
                getResetPass(UI.toString(editPass));
                break;
        }
    }
}
